﻿<script type="text/javascript">
    $(function () {
        getButton("relative");
    });

    function ui_relative_init_list(toolbar) {
        $("#ui_relative_dg").datagrid({       //初始化datagrid
            url: "ashx/bg_relative.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: false,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'name', title: '姓名', sortable: true, width: 100 },
                { field: 'tel', title: '联系电话', sortable: true, width: 150 }
            ]],
            columns: [[
                       { field: 'gender', title: '性别', sortable: true, width: 40 },
                       {
                           field: 'areaName', title: '村（镇）', sortable: true, width: 100
                       },
                        { field: 'cardtype', title: '证件类型', sortable: true, width: 100 },
                       { field: 'cardno', title: '证件号码', sortable: true, width: 150 },

                       { field: 'company', title: '工作单位', width: 150 },
                       

                       {
                           field: 'addr', title: '地址', width: 250,
                           formatter: function (value, row, index) {
                               if (!value) return "";
                               return value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                           }
                       },
                    { field: 'postcode', title: '邮编', width: 100 }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,   //请求回来的当前用户可以操作的按钮
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_relative_edit();
            }
        });
    }

    function ui_relative_add() {
        $("<div/>").dialog({
            id: "ui_relative_add_dialog",
            href: "html/ui_relative_edit.html",
            iconCls: "icon-add",
            title: "添加联系人",
            height: 300,
            width: 500,
            modal: true,
            buttons: [{
                id: "ui_relative_add_btn",
                text: '添加',
                iconCls: "icon-add",
                handler: function () {
                    $("#ui_relative_editform").form("submit", {
                        url: "ashx/bg_relative.ashx",
                        onSubmit: function (param) {
                            $('#ui_relative_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_relative_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval("(" + data + ")");    //转成json格式
                            if (dataJson.success) {
                                $("#ui_relative_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_relative_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_relative_add_btn').linkbutton('enable');  //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_relative_name_add").focus();
            },
            onClose: function () {
                $("#ui_relative_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_relative_edit() {
        var rows = $("#ui_relative_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的行或者双击某行");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_relative_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_relative_edit_dialog",
            href: "html/ui_relative_edit.html",
            iconCls: "icon-edit",
            title: "修改死者信息",
            height: 300,
            width: 500,
            modal: true,
            buttons: [{
                id: "ui_relative_edit_btn",
                text: '修 改',
                iconCls: "icon-edit",
                handler: function () {
                    $("#ui_relative_editform").form("submit", {
                        url: "ashx/bg_relative.ashx",
                        onSubmit: function (param) {
                            $('#ui_relative_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_relative_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = eval("(" + data + ")");    //转成json格式
                            if (dataJson.success) {
                                $("#ui_relative_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_relative_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $('#ui_relative_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                
                $("#ui_relative_id").val(row.id);
                $("#ui_relative_name").textbox('setValue', row.name);
                $("#ui_relative_cardno").textbox('setValue', row.cardno);
                $("#ui_relative_gender").combobox('setValue', row.gender);
                $('#ui_relative_tel').textbox('setValue', row.tel);
                $("#ui_relative_company").textbox('setValue', row.company);
                $("#ui_relative_title").textbox('setValue', row.title);
                $("#ui_relative_postcode").textbox('setValue', row.postcode);
                $("#ui_relative_areacode").combotree('setValue', row.areacode);
                $("#ui_relative_addr").textbox('setValue', row.addr);
            },
            onClose: function () {
                $("#ui_relative_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_relative_delete() {
        var rows = $("#ui_relative_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的行");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的' + rows.length + '行？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
                para.id = "";
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.id = ids.join(",");
                $.ajax({
                    url: "ashx/bg_relative.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", data.msg);
                            $("#ui_relative_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }


    function ui_relative_searchdata() {
        var whereStr = "1=1";
        var name = $('#ui_relative_search').find('[name=ui_relative_search_username]').val();
        var cardno = $('#ui_relative_search').find('[name=ui_relative_search_cardno]').val();
        var tel = $('#ui_relative_search').find('[name=ui_relative_search_tel]').val();
        var areaid = $('#ui_relative_search_area').combotree('getValue');
        
        if (name != "") {
            whereStr += " and name LIKE '%" + name + "%'";
        }
        if (cardno != "") {
            whereStr += " and cardno='" + cardno + "'";
        }
        if (tel != "") {
            whereStr += " and tel='" + tel + "'";
        }
        if (areaid != "") {
            var aid = areaid.substr(9, 3);
            if (aid == "000") {
                whereStr += " and left(areacode,9)='" + areaid.substr(0, 9) + "'";
            } else {
                whereStr += " and areacode='" + areaid + "'";
            }
        }
        
        $("#ui_relative_dg").datagrid('load', { where: whereStr });
        $("#ui_relative_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_relative_layout').layout('collapse', 'east');    //隐藏
    }
    function ui_relative_cleardata() {
        $('#ui_relative_search input').val('');
        $('#ui_relative_search select').val('select');        
        $("#ui_relative_dg").datagrid('load', {});

        $("#ui_relative_dg").datagrid('clearSelections').datagrid('clearChecked');
        //$('#ui_relative_layout').layout('collapse', 'east');    //隐藏
    }
</script>
<div id="ui_relative_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="ui_relative_dg" data-options="fit:true,border:false">
        </table>
    </div>
    <div data-options="region:'west',split:true,border:true,collapsed:true" title="多条件查询"
        style="width: 280px;">
        <div id="ui_relative_search">
            <table class="tableForm" style="width: 100%; height: 100%; background: #F5F5F5;">
                <tr>
                    <th>联系人姓名：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_relative_search_username" id="ui_relative_search_username" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr>
                <tr>
                    <th>证件号码：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_relative_search_cardno" id="ui_relative_search_cardno" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr> 
                <tr>
                    <th>联系电话：
                    </th>
                    <td>
                        <input class="easyui-textbox" name="ui_relative_search_tel" id="ui_relative_search_tel" autocomplete="off" style="width: 130px;" />
                    </td>
                </tr> 
                     
                <tr>
                    <th>所属地区：
                    </th>
                    <td>                        
                        <input class="easyui-combotree" id="ui_relative_search_area" name="ui_relative_search_area"
                            data-options="url:'ashx/bg_area.ashx?action=getall', lines:true" />
                    </td>
                </tr>                
                <tr>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                            onclick="ui_relative_searchdata();">搜索</a>
                    </td>
                    <td>
                        <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                            onclick="ui_relative_cleardata();">清空条件</a>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
